//
//  F2DAtlas.m
//  Framework2D
//
//  Created by Jorge Lorenzon on 5/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "F2DAtlas.h"

@implementation SpriteData

@end

@implementation F2DAtlas

@synthesize texture = _texture;

- (id) initWithContentsOfFile:(NSString*)file texture:(F2DTexture*)texture
{
    if (self = [super init])
    {
        _texture = texture;
        
        _sprites = [[NSMutableDictionary alloc] init];
        
        NSDictionary* atlasDataDict = [[NSMutableDictionary alloc] initWithContentsOfFile:file];
        
        NSDictionary* frames = [atlasDataDict objectForKey:@"frames"];
        
        NSEnumerator *enumerator = [frames keyEnumerator];
        id frameName;
        
        while ((frameName = [enumerator nextObject])) {
            SpriteData* spriteData = [[SpriteData alloc] init];
            
            spriteData->name = frameName;
            
            NSLog(@"%@",spriteData->name);
            
            NSDictionary* frameData = [frames objectForKey:frameName];
            
            spriteData->rect = CGRectFromString([frameData objectForKey:@"frame"]);
            
            [_sprites setObject:spriteData forKey:frameName];
        }
        
    }
    
    return self;
}

- (SpriteData*) spriteDataFrom:(NSString*)sprite
{
    return [_sprites objectForKey:sprite];
}

- (CGSize) size
{
    return CGSizeMake(_texture.width, _texture.height);
}

@end
